#!/bin/sh /etc/rc.common

START=99
STOP=10
USE_PROCD=1

. "$IPKG_INSTROOT/lib/functions/network.sh"
. "$IPKG_INSTROOT/etc/mihomo/scripts/constants.sh"

start_service() {
	# clear log
	clear_log
	# load config
	config_load mihomo
	# check if enabled
	local enabled
	config_get_bool enabled "config" "enabled" 0
	if [ "$enabled" == 0 ]; then
		log "Mox is disabled."
		log "Exiting..."
		return
	fi
	log "Mox is enabled."
	log "Starting..."
	# get config
	## app config

	log "Start Successful!"
}

service_stopped() {
	cleanup
}

reload_service() {
	cleanup
	start
}

service_triggers() {
	procd_add_reload_trigger "mox"
}

cleanup() {
	# delete cron
	sed -i '/#mox/d' "/etc/crontabs/root" > /dev/null 2>&1
	/etc/init.d/cron restart
}

log() {
	echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$APP_LOG_PATH"
}

clear_log() {
	if [ -d "$LOG_DIR" ]; then
		rm -rf "$LOG_DIR"
	fi
	mkdir "$LOG_DIR"
}

